#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" By Martin Senande-Rivera
    For Towards and atmosphere more favourable to firestorm development in Europe """

import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from scipy import stats
import matplotlib.ticker as mtick
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from matplotlib.ticker import FormatStrFormatter


font = {'size'   : 5}
matplotlib.rc('font', **font)

# Read files
events = pd.read_csv('../1-Fire_simulations/simulations_results.csv', parse_dates=['Date'])

x = ['Ki']
y = ['downdraft_max (m s-1)']
xlabel = ['K-index ($C^{\circ}$)']

fig = plt.figure(figsize=(7.5,3))
gs = fig.add_gridspec(1,2)

# 3d plot of downdraft intensity, fuel load and K-index
ax1 = fig.add_subplot(gs[0,0],projection='3d')
xi = 0
yi=0
X = x[xi]
Y = y[yi]
X_f03 = events[(events['FUEL']==3)].reset_index()[X]
Y_f03 = -events[(events['FUEL']==3)].reset_index()[Y].dropna()
Z_f03 = events[(events['FUEL']==3)].reset_index()['FUEL']*0.+0.675     # Fuel load in kg m-2 of Anderson tall grass category
X_f06 = events[(events['FUEL']==6)].reset_index()[X]
Y_f06 = -events[(events['FUEL']==6)].reset_index()[Y].dropna()
Z_f06 = events[(events['FUEL']==6)].reset_index()['FUEL']*0.+1.345     # Fuel load in kg m-2 of Anderson shrub category
X_f10 = events[(events['FUEL']==10)].reset_index()[X]
Y_f10 = -events[(events['FUEL']==10)].reset_index()[Y].dropna()
Z_f10 = events[(events['FUEL']==10)].reset_index()['FUEL']*0.+2.694    # Fuel load in kg m-2 of Anderson timber litter category
X_f12 = events[(events['FUEL']==12)].reset_index()[X]
Y_f12 = -events[(events['FUEL']==12)].reset_index()[Y].dropna()
Z_f12 = events[(events['FUEL']==12)].reset_index()['FUEL']*0.+7.749    # Fuel load in kg m-2 of Anderson medium logging slash category
X_f13 = events[(events['FUEL']==13)].reset_index()[X]
Y_f13 = -events[(events['FUEL']==13)].reset_index()[Y].dropna()
Z_f13 = events[(events['FUEL']==13)].reset_index()['FUEL']*0.+13.024   # Fuel load in kg m-2 of Anderson heavy logging slash category
plt.gca().invert_zaxis()
ax1.scatter(Y_f03, X_f03, Z_f03, color='b', s=20, label='Tall grass')
ax1.scatter(Y_f06, X_f06, Z_f06, color='g', s=20, label='Shrub')
ax1.scatter(Y_f10, X_f10, Z_f10, color='y', s=20, label='Timber litter')
ax1.scatter(Y_f12, X_f12, Z_f12, color='r', s=20, label='Medium logging slash')
ax1.scatter(Y_f13, X_f13, Z_f13, color='darkred', s=20, label='Heavy logging slash')
plt.yticks([-45.,-25.,0.,25.,45.])
ax1.tick_params(pad=-2)
ax1.set_zlim(13.024,0.)
ax1.set_xlabel('Downdraft ($m\cdot s^{-1}$)', labelpad=-5)
ax1.set_ylabel(xlabel[xi], labelpad=-5)
ax1.set_zlabel('Fuel load ($kg\cdot m^{-2}$)', labelpad=-5)
ax1.grid(True)
ax1.legend(bbox_to_anchor=(.1, 1.0, 1.0, .05),fontsize=5,
        ncol=3, fancybox=True, shadow=False)
ax1.text2D(0.09, 0.97, 'a',family='sans-serif',weight='bold',size=7, horizontalalignment='left', verticalalignment='top',transform=fig.transFigure)
plt.gcf()

# Boxplots of K-index against downdraft intensity
ax2 = fig.add_subplot(gs[0,1])
xi = 0
yi=0
X = x[xi]
Y = y[yi]
X_strong = events[(events[y[yi]]<-8.)].reset_index()[X].dropna()
X_medium = events[(events[y[yi]]>=-8.) & (events[y[yi]]<-4.)].reset_index()[X].dropna()
X_weak = events[(events[y[yi]]>=-4.)].reset_index()[X].dropna()
bp1 = plt.boxplot([X_weak],sym='', positions=[1.],patch_artist=True)
bp1['boxes'][0].set(facecolor='dimgray')   
bp2 = plt.boxplot([X_medium],sym='', positions=[2.],patch_artist=True)
bp2['boxes'][0].set(facecolor='darkgray')   
bp3 = plt.boxplot([X_strong],sym='', positions=[3.],patch_artist=True)
bp3['boxes'][0].set(facecolor='indigo')   
plt.ylabel(xlabel[xi])
plt.xticks([1.,2.,3.], ['Downdraft<4 $m\cdot s^{-1}$', '4<Downdraft<8 $m\cdot s^{-1}$', 'Downdraft>8 $m\cdot s^{-1}$'])
ax2.grid(True)
ax2.axhline(25.,c='grey', ls='--',lw=1)
ax2.text(0.53, 0.97, 'b',family='sans-serif',weight='bold',size=7, horizontalalignment='left', verticalalignment='top',transform=fig.transFigure)
plt.gcf()

# Save figure
plt.subplots_adjust(top = 0.9, bottom=0.1, hspace=0.2, wspace=0.4)
fig.savefig('Figure2.png',dpi=300,bbox_inches='tight')   
fig.savefig('Figure2.pdf',bbox_inches='tight')   
